package com.example.lirisheng.springbootfinalexperiment.security.filter;


import com.example.lirisheng.springbootfinalexperiment.repositories.UserRoleRepository;
import com.example.lirisheng.springbootfinalexperiment.repositories.UsersRepository;
import com.example.lirisheng.springbootfinalexperiment.security.provider.WeiAuthenticationProvider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.lang.Nullable;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.AuthenticationServiceException;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
import org.springframework.security.web.util.matcher.AntPathRequestMatcher;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 继承AbstractAuthenticationProcessingFilter来定义微信端的登录验证filter
 * @Component(不要把其注入容器中,其会被识别为filter,加入web filter chain中)
 */
public class WeiAuthenticaitonFilter  extends AbstractAuthenticationProcessingFilter {

    @Autowired
    WeiAuthenticationProvider weiAuthenticationProvider;

    private String codeParameter = "code";
    private boolean postOnly = true;

    @Autowired
    UsersRepository usersRepository;

    @Autowired
    UserRoleRepository userRoleRepository;


    public WeiAuthenticaitonFilter() {

        super(new AntPathRequestMatcher("/weiLogin", "POST"));
        super.setAuthenticationManager(new AuthenticationManager() {
            @Override
            public Authentication authenticate(Authentication authentication) throws AuthenticationException {
                return null;
            }
        });
    }

    @Override
    public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response) throws AuthenticationException {
        if (this.postOnly && !request.getMethod().equals("POST")) {
            throw new AuthenticationServiceException("Authentication method not supported: " + request.getMethod());
        } else {
            String code = this.obtainCode(request);

            if (code == null) {
                code = "";
            }

            code = code.trim();

            //查出权限 ,构造一个authentication,返回给前端
            UsernamePasswordAuthenticationToken authRequest = new UsernamePasswordAuthenticationToken(code, null);
//            this.setDetails(request, authRequest);
            return weiAuthenticationProvider.authenticate(authRequest);

        }
    }

    @Nullable
    protected String obtainCode(HttpServletRequest request) {
        return request.getParameter(this.codeParameter);
    }

    @Override
    public void setAuthenticationFailureHandler(AuthenticationFailureHandler failureHandler) {
        super.setAuthenticationFailureHandler(failureHandler);
    }

    @Override
    public void setAuthenticationSuccessHandler(AuthenticationSuccessHandler successHandler) {
        super.setAuthenticationSuccessHandler(successHandler);
    }


    @Override
    public void setFilterProcessesUrl(String filterProcessesUrl) {
        super.setFilterProcessesUrl(filterProcessesUrl);
    }
}
